<template>
  <div class="modal" v-show="$store.state.isOpenLogin">
    <div class="modal_bg" @click="closeLogin"></div>
    <div class="modal_content">
      <img
        class="close"
        @click="closeLogin"
        src="../assets/images/login/userArrow.png"
        alt=""
      />
      <section>
        <span class="active">手机号登录</span>
        <span>微信扫码登录</span>
      </section>
      <div class="phone_login">
        <div class="line tanxin">
          <input
            type="text"
            placeholder="请输入手机号"
            v-model="phone"
            @input="checkingPhone"
          />
          <span
            class="checkPhone"
            :style="{ color: TipsColor == false ? 'red' : 'green' }"
          >
            {{ Tips }}
          </span>
        </div>
        <div class="verify_box">
          <slide-verify
            :l="42"
            :r="10"
            :w="362"
            :h="140"
            :accuracy="8"
            slider-text="向右滑动"
            @success="onSuccess"
            @fail="onFail"
            @refresh="onRefresh"
            :style="{ width: '100%' }"
            class="slide-box"
            ref="slideBlock"
          ></slide-verify>
          <div :style="{ color: slideCheck == false ? 'red' : 'green' }">
            {{ msg }}
          </div>
        </div>

        <div class="line tanxin">
          <input type="text" placeholder="请输入短信验证码" v-model="code" />
          <button class="btn" @click="getCode" ref="getcode">获取验证码</button>
        </div>
        <div class="btn_login">登录</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      msg: "",
      phone: "",
      Tips: "",
      TipsColor: false,
      code: "",
      slideCheck: "",
    };
  },
  methods: {
    //   关闭这个登录框
    closeLogin() {
      this.$store.commit("changeLoginShow", false);
    },
    // 拼图成功的时候
    onSuccess(times) {
      console.log("验证通过，耗时" + times + "毫秒");
      this.msg = `login success,耗时${(times / 1000).toFixed(1)}s`;
      this.slideCheck = true;
    },
    // 拼图失败的时候
    onFail() {
      this.msg = "";
      this.slideCheck = false;
    },
    // 拼图刷新的时候
    onRefresh() {
      this.msg = "";
    },

    checkingPhone() {
      if (this.phone != "") {
        if (!/^1[3456789]\d{9}$/.test(this.phone)) {
          this.Tips = "请填写正确的手机号码！";
          this.TipsColor = false;
        } else {
          this.Tips = "手机号码可以使用！";
          this.TipsColor = true;
        }
      }
    },
    getCode() {
      if (this.TipsColor === true) {
        if (this.slideCheck === true) {
          let suijishu = Math.floor(
            Math.random() * (999999 - 100000 + 1) + 100000
          );
          this.code = suijishu;
          let time = 60;
          let timer = setInterval(() => {
            if (time == 0) {
              clearInterval(timer);
              this.$refs.getCode.disabled = false;
              this.$refs.getCode.style = "background:#0A328E";
              this.$refs.getCode.innerText = "获取验证码";
              time = 60;
            } else {
              this.$refs.getCode.innerText = "还剩" + time + "秒";
              time--;
            }
          }, 1000);
        } else {
          this.msg = "请先验证";
        }
      } else {
        this.Tips = "请输入正确的手机号";
      }
    },
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/base.less";
.modal {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;

  .modal_bg {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
  }
  .modal_content {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    margin: auto;
    width: 555px;
    height: 423px;
    background: url(../assets/images/login/loginBg.png);
    .close {
      position: absolute;
      right: 60px;
      top: 15px;
      cursor: pointer;
    }
    section {
      display: flex;
      justify-content: center;
      padding-top: 40px;
      margin-bottom: 30px;
      span {
        color: #ccc;
        cursor: pointer;
        &:nth-of-type(1) {
          border-right: 1px solid #ccc;
          padding-right: 10px;
          margin-right: 10px;
        }
        &.active {
          color: #000;
        }
      }
    }
    .line {
      width: 360px;
      margin: 0 auto 20px;
      height: 50px;
      input {
        flex: 1;
        text-indent: 1em;
      }
      .btn {
        color: #fff;
        background-color: @blue;
        text-align: center;
        width: 90px;
        height: 50px;
        line-height: 50px;
        cursor: pointer;
      }
    }
    .btn_login {
      width: 360px;
      height: 50px;
      background-color: @blue;
      color: #fff;
      line-height: 50px;
      margin: auto;
      text-align: center;
      cursor: pointer;
    }
  }
}
.verify_box {
  width: 360px;
  margin: 0 auto 20px;
  /deep/.slide-box {
    width: 100%;
    position: relative;
    box-sizing: border-box;
    canvas {
      position: absolute;
      left: 0;
      top: -140px;
      display: none;
      width: 100%;
      box-sizing: border-box;
    }
    .slide-verify-block {
      width: 70px;
      height: 135px;
    }
    &:hover {
      canvas {
        display: block;
      }
    }
  }
}
</style>